package com.platform.api;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.UserEntity;
import com.platform.util.ApiBaseAction;
import com.platform.utils.MapUtils;
import com.platform.utils.R;
import com.platform.utils.StringUtils;

import io.swagger.annotations.Api;

@Api(tags = "API用户信息接口")
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends ApiBaseAction{
	
    @ResponseBody
    @RequestMapping(value = "/login")
    public R login(
    		HttpServletRequest request, HttpServletResponse response) {
        String code = request.getParameter("code");
        String password = request.getParameter("password");
        if (!StringUtils.isNullOrEmpty(code)) {
            try {
                UserEntity user = userService.loginByPassword(code,password);
        	    Map<String, Object> tokenMap = tokenService.createToken(user.getId());
        	    String token = MapUtils.getString("token",tokenMap);
        		return R.ok().put("user", user).put("token", token).put("expireTime", tokenMap.get("expireTime"));
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
				return R.error(e.getMessage());
			}
        }else {
			logger.error("code is null");
			return R.error("code is null");
        }
    }

}
